package org.gwu.seuss.server.command;

import org.gwu.seuss.communication.request.Actions;
import org.gwu.seuss.communication.request.ClientRequest;
import org.gwu.seuss.communication.request.ServerResponse;
import org.springframework.util.Assert;

public class DeleteFileCommand extends UserCommandBase implements Command {
	
	public DeleteFileCommand(ClientRequest req) {
		super(req);
	}
	
	@Override
	public ServerResponse execute() throws Exception {
		String username = req.getUsername();
		String password = req.getPassword();
		String filename = req.getFileName();
		
		Assert.hasText(username, "Username is not provided.");
		Assert.hasText(password, "Password is not provided.");
		Assert.hasText(filename, "Filename is not provided.");

		userService.actionAllowed(username, Actions.DELETE_FILE);
		log.info("User " + username + "requested to delete " + filename);
		
		int result = fileService.deleteFile(username, filename);
		
		return getServerResponse(result, "Deleted " + filename);
	}
}